package com.nys.cqhttp.message.factory.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

import com.nys.cqhttp.message.factory.IMessageHandler;

import com.nys.cqhttp.model.Massage;
import com.nys.cqhttp.param.Context;

import com.nys.cqhttp.param.MessageParam;
import lombok.extern.slf4j.Slf4j;

/**
 * DefaultMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 消息默认处理
 * @date 2021/12/28 15:44
 */
@Slf4j
public class DefaultMessageHandler implements IMessageHandler {

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @Override
  public String handler(Massage messagePojo, Context context) {
    try {
      String[] message = messagePojo.getValue().split("&");
      MessageParam messageParam = context.getMessageParam();
      Random random = new Random();
      log.info(message.toString());
      String res = "";
      if (messageParam.getMessage().equals("approve")) {
        if (messageParam.getSenderId().equals(messageParam.getSelfId()))
          res = at + messageParam.getSenderId() + at1;
      } else if (messageParam.getMessage().equals("kick")) {
        res = messageParam.getSenderId() + "被" + messageParam.getOperatorId();
      } else if (messageParam.getMessage().equals("leave")) {
        res = messageParam.getSenderId();
      }
      res += message.length == 1 ? message[0] : message[random.nextInt(message.length)];
      return res;
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }
}
